package cn.cstech.aijia.crash;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.harwkin.nb.camera.FileUtil;

import cn.cstech.aijia.R;
import cn.cstech.aijia.common.AppDebug;
import cn.cstech.aijia.common.DirConstants;
import cn.cstech.aijia.ui.base.BaseActivity;
import cn.cstech.aijia.ui.base.utils.DateUtil;

public class BugReportActivity extends BaseActivity implements ErrorKeys {
	private Button mBtnReboot;
	private LinearLayout mDebugInfoPage;
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.bug_report_view);
		
		mDebugInfoPage = (LinearLayout)findViewById(R.id.debug_info);

		final StringBuilder reportText = new StringBuilder();
		reportText.append("包名:").append(getPackageName()).append("\n");
		reportText.append("手机型号:").append(Build.MODEL).append("\n");
		reportText.append("手机设备名:").append(Build.DEVICE).append("\n");
		reportText.append("产品名:").append(Build.PRODUCT).append("\n");
		reportText.append("手机厂商名:").append(Build.MANUFACTURER).append("\n");
		reportText.append("系统版本号:").append(Build.VERSION.RELEASE).append("\n");
		reportText.append(getIntent().getStringExtra(STACKTRACE));
		
		if(AppDebug.DEBUG_LOG){
			FileUtil.writeContent(DirConstants.DIR_LOGS + DateUtil.getDisplayDate(System.currentTimeMillis(), "yyyyMMddHHmmss") + ".log", reportText.toString());
			
			initDebugInfo(reportText.toString());
		}
	}
	
	private void initDebugInfo(final String errorReport){
		mDebugInfoPage.setVisibility(View.VISIBLE);

		setTitleText(getString(R.string.label_crash_name));
		
		final TextView reportTextView = (TextView)findViewById(R.id.report_text);
		reportTextView.setMovementMethod(ScrollingMovementMethod.getInstance());
		reportTextView.setClickable(false);
		reportTextView.setLongClickable(false);

		final String versionName = new ErrorUtil(this).getVersionName();
		reportTextView.append(String.format(getString(R.string.hint_crash), versionName));
		reportTextView.append(errorReport);

		findViewById(R.id.send_report).setOnClickListener(
			new OnClickListener() {
				public void onClick(View view) {
					Intent sendIntent = new Intent(Intent.ACTION_SEND);
					sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "1540346532@qq.cn" });
					sendIntent.putExtra(Intent.EXTRA_TEXT, errorReport);
					sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Spriter " + versionName + " exception report");
					sendIntent.setType("message/rfc822");
					startActivity(sendIntent);
					finish();
				}
			}
		);

		findViewById(R.id.cancel_report).setOnClickListener(
				new OnClickListener() {
					public void onClick(View view) {
						finish();
					}
				}
		);
	}
}
